package com.sky.controller.admin;

import com.sky.dto.CategoryDTO;
import com.sky.dto.CategoryPageQueryDTO;
import com.sky.result.PageResult;
import com.sky.result.Result;
import com.sky.service.CategoryService;
import com.sky.vo.CategoryVo;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * 分类管理
 *
 * @Author mr.zhang
 * Date 2025/5/18 20:46
 */
@RestController
@RequestMapping("/admin/category")
@Slf4j
@Api(tags = "分类相关接口")
public class CategoryController {

    @Autowired
    private CategoryService categoryService;

    /**
     * 新增分类
     */
    @PostMapping
    @ApiOperation(value = "新增分类")
    public Result save(@RequestBody CategoryDTO categoryDTO){
        log.info("新增分类:{}",categoryDTO);
        categoryService.save(categoryDTO);
        return Result.success();
    }

    /**
     * 分页查询
     */
    @GetMapping("/page")
    @ApiOperation(value = "分页查询")
    public Result<PageResult> pageList(CategoryPageQueryDTO dto){
        log.info("分页查询参数数据:{}",dto);
        PageResult pageResult = categoryService.pageList(dto);
        return Result.success(pageResult);
    }


    /**
     * 删除分类
     */
    @DeleteMapping
    @ApiOperation(value = "删除分类")
    public Result deleteById(@RequestParam Integer id){
        log.info("删除分类:{}",id);
        try {
            categoryService.deleteById(id);
        } catch (Exception e) {
            return Result.error(e.getMessage());
        }
        return Result.success();
    }


    /**
     * 修改分类
     */
    @PutMapping
    @ApiOperation(value = "修改分类")
    public Result update(@RequestBody CategoryDTO categoryDTO){
        categoryService.update(categoryDTO);
        return Result.success();
    }

    /**
     * 根据类型查询分类
     */
    @GetMapping("/list")
    @ApiOperation(value = "根据类型查询分类")
    public Result listByType(@RequestParam("type") Integer type){
        List<CategoryVo> categoryVoList = categoryService.listByType(type);
        log.info("根据类型查询分类:{}",categoryVoList);
        return Result.success(categoryVoList);
    }

}
